package servlet;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import datos.clases.ProcesaUsuario;

public class FrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
    public FrontController() {    	
        super();        
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String res = (String)request.getParameter("res");
		if(res.equals("confirmarRegistro.jsp")){
			RequestDispatcher d = request.getRequestDispatcher(res);			
			d.forward(request,response);
		}else if(res.equals("salir")){
			HttpSession sesion = request.getSession();				
			sesion.invalidate();			
			RequestDispatcher d = request.getRequestDispatcher("index.html");
			d.forward(request,response);
		}else{
			String user = "";
			String pass = "";
			HttpSession sesion = request.getSession(false);
			if(sesion != null){
				user = (String)sesion.getAttribute("user");				
				pass = (String)sesion.getAttribute("pass");
				if(user!=null && pass!=null){
					RequestDispatcher d = request.getRequestDispatcher(res);
					d.forward(request,response);
				}else{
					user = (String)request.getParameter("user");
					pass = (String)request.getParameter("pass");	
					boolean login = (new ProcesaUsuario().comprueba(user,pass));		
					if(login){		
						sesion = request.getSession();
						sesion.setAttribute("user",user);
						sesion.setAttribute("pass",pass);
						RequestDispatcher d = request.getRequestDispatcher(res);
						d.forward(request,response);
					}else{						
						RequestDispatcher d = request.getRequestDispatcher("error.jsp?motivo=Nombre de usuario o contraseña incorrectos");
						d.forward(request,response);	
					}
				}
			}else{
				user = (String)request.getParameter("user");
				pass = (String)request.getParameter("pass");	
				boolean login = (new ProcesaUsuario().comprueba(user,pass));		
				if(login){					
					//sleep();
					sesion = request.getSession();
					sesion.setAttribute("user",user);
					sesion.setAttribute("pass",pass);
					RequestDispatcher d = request.getRequestDispatcher(res);
					d.forward(request,response);
					
				}else{
					RequestDispatcher d = request.getRequestDispatcher("error.jsp?motivo=Nombre de usuario o contraseña incorrectos");
					d.forward(request,response);	
				}
			}										
			}
	}
	
	
	protected void sleep(){				
		try{			
			Thread.sleep(2000);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
	}	

}
